package com.region.jms;

 
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import com.common.util.ObjectUtils;
import com.core.service.AppBaseService; 
import com.region.entity.TB_REGION_TP;
import com.region.entity.TB_REGION_XX;

/**
 * 生成网页内容
 *
 */
@MessageDriven(activationConfig={
		@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue"),
		@ActivationConfigProperty(propertyName="destination",propertyValue="ReleaseDqxxQueue")
	})
public class JMB_RELEASE_DQXX  extends AppBaseService implements MessageListener {
 
	 
    public void onMessage(Message message) {
    	TextMessage tm = (TextMessage)message;
		String content="";
		try {
			content = tm.getText();
		} catch (JMSException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Map param =jsonParam(content);   
    	try {
		
			String CMS_WS_URL = (String) param.get("CMS_WS_URL");
			String path_date = ObjectUtils.dateToString(getOpTime(param), "yyyyMMdd"); 
			String path_root = "upload/images/qyxx/"+path_date;
			
			//处理图片
			StringBuffer hql = new StringBuffer("select o from TB_REGION_TP o where o.QYID=:QYID");
			List<TB_REGION_TP> tp_list = (List<TB_REGION_TP>) getResultList(hql.toString(),param);
			for(TB_REGION_TP item : tp_list){
				String path =  item.getFILE_PATH();
				Map TB_UFM_UPLOADFILE = (Map) getServiceInterface().doService("SVC_UPLOADFILE", "GET_FILE_BY_ID", item.getFILE_ID());
				if(path!=null) {
					getServiceInterface().doWebService(CMS_WS_URL, "deleteFile", path);
				}
				path=path_root+"/"+item.getFILE_ID()+TB_UFM_UPLOADFILE.get("EXT_NAME"); 
				getServiceInterface().doWebService(CMS_WS_URL, "releaseImage", path, TB_UFM_UPLOADFILE.get("FILE_BODY"));
				 
				item.setFILE_PATH(path) ;
				getJpaDao().update(item);
			}
			
			//处理生成文件
			hql = new StringBuffer("select o from TB_REGION_XX o where o.QYID=:QYID and o.XXBM='HTML_FILE_PATH'");
			String htmlfilepath = "html/00/"+path_date+"/"+param.get("QYID")+".html";
			TB_REGION_XX xx = (TB_REGION_XX) querySingleResult(hql.toString(), param);
			if(xx==null){
				xx = new TB_REGION_XX();
				xx.setXH(-1);
				xx.setTYPE("PROVINCE");
				xx.setQYID((String)param.get("QYID"));
				xx.setXXBM("HTML_FILE_PATH");
				xx.setXXMC("HTML文件URL");
				xx.setXXXS("hidden");
				xx.setXXZ(htmlfilepath);
				xx.setID(xx.uuid());
				xx.setCREATE_TIME(getOpTime(param));
				xx.setOP_ITEM(getOpTime(param));
				xx.setOPID(getOpid(param));
				getJpaDao().save(xx);
			}else{
				xx.setXXZ(htmlfilepath);
				xx.setOP_ITEM(getOpTime(param));
				xx.setOPID(getOpid(param));
				getJpaDao().update(xx);
			}
			
			//更新状态
			 HashMap inParam = new HashMap();
	         copySysParam(param, inParam);
	         inParam.put("PROVINCE_CODE",param.get("QYID"));
	         inParam.put("STATUS", 10);
	         getServiceInterface().doService("SVC_BASE", "UPD_PROVINCES_STATUS", inParam); 
			
		}  catch(Exception e) {
			//更新状态
			 HashMap inParam = new HashMap();
	         copySysParam(param, inParam);
	         inParam.put("PROVINCE_CODE", param.get("QYID"));
	         inParam.put("STATUS", 10);
	         getServiceInterface().doService("SVC_BASE", "UPD_PROVINCES_STATUS", inParam); 
	         
			e.printStackTrace();
		}
    	 
    }
    
    

}
